package unit;

import junit.framework.TestCase;

import com.google.code.netcheckin.aprs.AprsFormatter;
import com.google.code.netcheckin.aprs.AprsPacketDataObject;
import com.google.code.netcheckin.aprs.LatOrLong;
import com.google.code.netcheckin.io.Ascii;
import com.google.code.netcheckin.util.ByteUtils;

public class UAprsPacketDataObject extends TestCase {

	protected TestAprsPacketDataObject aprsObject;
	protected String defaultGuts = ";myobject *" + UAprsFormatter.CORRECT_STAMP
		+ "3917.65N\\08200.15W/howdy do da";
	protected String defaultPacket = "FROM>TO:PATH" + defaultGuts;

	public UAprsPacketDataObject(String name) {
		super(name);
	}

	public void setUp() {
		aprsObject = new TestAprsPacketDataObject(new AprsFormatter());
		aprsObject.setSourceAddress("FROM".getBytes());
		aprsObject.setDestAddress("TO".getBytes());
		aprsObject.setPath("PATH".getBytes());
		aprsObject.setObjectName("myobject".getBytes());
		aprsObject.setSymbolTable(Ascii.V_BACK_SLASH);
		aprsObject.setSymbolId(Ascii.V_SLASH);
		aprsObject.setTimestamp(UAprsFormatter.ASTAMP);
		aprsObject.setObjectStatus(AprsPacketDataObject.OBJECT_CREATE);
		aprsObject.setLatitude(new LatOrLong(39, 17.65, Ascii.V_N, 0));
		aprsObject.setLongitude(new LatOrLong(82, 0.15, Ascii.V_W, 0));
		aprsObject.setComment("howdy do da");
	}

	public void tearDown() {
		aprsObject = null;
	}

	public void testAAAddPacketBytesBasic() {
		byte[] result = new byte[256];
		int offset = aprsObject.testAddPacketBytes(result, 0);
		String answer = new String(ByteUtils.subArray(result, 0, offset));
		assertEquals("wrong data", defaultGuts, answer);
	}

	public void testAABddPacketBytesNoComment() {
		byte[] result = new byte[256];
		aprsObject.setComment((byte[])null);
		int offset = aprsObject.testAddPacketBytes(result, 0);
		String answer = new String(ByteUtils.subArray(result, 0, offset));
		int idx = defaultGuts.indexOf("howdy do da");
		String correct = defaultGuts.substring(0, idx);
		assertEquals("wrong data", correct, answer);
	}

	public void testAFToPacketBytes() {
		byte[] result = aprsObject.toPacket();
		String answer = new String(result);
		assertEquals("wrong data", defaultPacket, answer);
	}

	protected class TestAprsPacketDataObject extends AprsPacketDataObject {
		public TestAprsPacketDataObject() {
			super();
		}
		public TestAprsPacketDataObject(AprsFormatter formatter) {
			super(formatter);
		}

		public int testAddPacketBytes(byte[] buffer, int offset) {
			return addPacketBytes(buffer, offset);
		}
	}
}
